home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dld / dld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  58.1 KB  |  2,130 lines

  1. /* dld -- dynamic link/unlink editor for C
  2.    Copyright (C) 1990 by W. Wilson Ho.
  3.  
  4.    Version 3.2.3
  5.    The author can be reached electronically by how@cs.ucdavis.edu or
  6.    through physical mail at:
  7.  
  8.    W. Wilson Ho
  9.    Division of Computer Science
  10.    University of California at Davis
  11.    Davis, CA 95616
  12.  */
  13.  
  14. /* This program is free software; you can redistribute it and/or modify it
  15.    under the terms of the GNU General Public License as published by the
  16.    Free Software Foundation; either version 1, or (at your option) any
  17.    later version.
  18.  
  19.    This program borrows and modifies a number of functions and data
  20.    structures from the implementation of the GNU `ld' link editor.  The
  21.    original copyleft notice from the GNU `ld' is also included. */
  22.  
  23. /* Linker `ld' for GNU
  24.    Copyright (C) 1988 Free Software Foundation, Inc.
  25.  
  26.    This program is free software; you can redistribute it and/or modify
  27.    it under the terms of the GNU General Public License as published by
  28.    the Free Software Foundation; either version 1, or (at your option)
  29.    any later version.
  30.  
  31.    This program is distributed in the hope that it will be useful,
  32.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.    GNU General Public License for more details.
  35.  
  36.    You should have received a copy of the GNU General Public License
  37.    along with this program; if not, write to the Free Software
  38.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  39.  
  40. #include "defs.h"
  41.  
  42. #if 0
  43. /*
  44.  * This part is taken out after the elimination of use of alloca().
  45.  */
  46. /* 
  47.  * Alloca include.
  48.  */
  49. /* If compiled with GNU C, use the built-in alloca */
  50. #ifdef __GNUC__
  51. #define alloca __builtin_alloca
  52. #endif
  53.  
  54. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  55. #include <alloca.h>
  56. #endif
  57. #endif
  58.  
  59. /* System dependencies */
  60.  
  61. #ifdef atarist
  62. #include <basepage.h>
  63. /* _initial_stack determines the amount of memory available as stack/heap,  */
  64. /* if it's negative this is the amount available to the system, 1L means    */
  65. /* use a ratio of 1:3 (stack:system), 2L means ratio 1:1, 3L stands for 3:1 */
  66. static char __patch_str[] = "{PatchVar}stack = %ld bytes";
  67. extern long _initial_stack = - 100 * 1024L;
  68. #endif /* atarist */
  69.  
  70. /* Ordinary 4.3 bsd lacks these macros in a.out.h */
  71.     
  72. #ifndef N_TXTADDR
  73. #ifdef vax
  74. #define N_TXTADDR(x) 0
  75. #endif
  76. #ifdef sequent
  77. #define N_TXTADDR(x) (N_ADDRADJ(x))
  78. #endif
  79. #endif
  80.  
  81. #ifndef N_DATADDR
  82. #ifdef vax
  83. #define N_DATADDR(x) \
  84.     (((x).a_magic == OMAGIC) ? (N_TXTADDR(x)+(x).a_text) \
  85.      : (page_size + ((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  86. #endif
  87. #ifdef sequent
  88. #define N_DATADDR(x) \
  89.         (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  90.         : (page_size+(((x).a_text-1) & ~(page_size-1))))
  91. #endif
  92. #endif
  93.  
  94. #ifndef N_BSSADDR
  95. #ifdef vax
  96. #define N_BSSADDR(x) (N_DATADDR(x)+(x).a_data)
  97. #endif
  98. #endif
  99.  
  100. /*
  101.  * Ok.  Following are the relocation information macros.  If your
  102.  * system cannot use the default set (below), you must define all of these:
  103.  
  104.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  105.  * of structure that is stored in the relocation info section of your
  106.  * a.out files.  Often this is defined in the a.out.h for your system.
  107.  *
  108.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  109.  * <whatever> to be relocated.  *Must be an lvalue*.
  110.  *
  111.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  112.  * external symbol (1), or was it fully resolved upon entering the
  113.  * loader (0) in which case some combination of the value in memory
  114.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  115.  * what the value of the relocation actually was.  *Must be an lvalue*.
  116.  *
  117.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  118.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  119.  *
  120.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  121.  * pc relative.
  122.  *
  123.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  124.  * present in the memory location to be relocated is *subtracted*
  125.  * from the relocation value, to produce the final result.
  126.  * By default, this is always 0.
  127.  *
  128.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  129.  * an extra value to be added to the relocation value based on the
  130.  * individual relocation entry.  *Must be an lvalue if defined*.
  131.  *
  132.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  133.  * final relocation value before putting it where it belongs.
  134.  *
  135.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  136.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  137.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  138.  * do everything in terms of the bit operators below), but having this
  139.  * macro could end up producing better code on machines without fancy
  140.  * bit twiddling.  Also, it's easier to understand/code big/little
  141.  * endian distinctions with this macro.
  142.  *
  143.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  144.  * with the bits of the relocation value.  It may be assumed by the
  145.  * code that the relocation value will fit into this many bits.  This
  146.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  147.  *
  148.  *
  149.  *        Things I haven't implemented
  150.  *        ----------------------------
  151.  *
  152.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  153.  *
  154.  *    Pc relative relocation for External references.
  155.  *
  156.  *
  157.  */
  158.  
  159. #if defined(sun) && defined(sparc)
  160. /* Sparc (Sun 4) macros */
  161. #undef relocation_info
  162. #define relocation_info                    reloc_info_sparc
  163. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  164. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  165. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  166. #define RELOC_MEMORY_SUB_P(r)        0
  167. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  168. #define RELOC_PCREL_P(r)             \
  169.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  170. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  171. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  172. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  173.  
  174. /* Note that these are very dependent on the order of the enums in
  175.    enum reloc_type (in a.out.h); if they change the following must be
  176.    changed */
  177. /* Also note that the last few may be incorrect; I have no information */
  178. static int reloc_target_rightshift[] = {
  179.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  180. };
  181. static int reloc_target_size[] = {
  182.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  183. };
  184. static int reloc_target_bitsize[] = {
  185.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  186. };
  187.  
  188. #endif
  189.  
  190. #if defined(sequent)
  191. #define RELOC_ADDRESS(r)        ((r)->r_address)
  192. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  193. #define RELOC_SYMBOL(r)            ((r)->r_symbolnum)
  194. #define RELOC_MEMORY_SUB_P(r)        ((r)->r_bsr)
  195. #undef RELOC_ADD_EXTRA
  196. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  197. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  198. #endif
  199.  
  200. /* Default macros */
  201. #ifndef RELOC_ADDRESS
  202. #define RELOC_ADDRESS(r)        ((r)->r_address)
  203. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  204. #define RELOC_SYMBOL(r)            ((r)->r_symbolnum)
  205. #define RELOC_MEMORY_SUB_P(r)        0
  206. #undef RELOC_ADD_EXTRA
  207. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  208. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  209. #endif
  210.  
  211. /* Size of a page; obtained from the operating system. */
  212.  
  213. static int page_size;
  214.  
  215. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  216. symbol *_dld_symtab[TABSIZE];
  217.  
  218. /* Count the number of global symbols referenced and not defined.  */
  219. int dld_undefined_sym_count = 0;
  220.  
  221. /* internal format of relocation info entry. */
  222. struct dld_reloc_info {
  223.     /* corresponding symbol table entry. */
  224.     symbol *sp;
  225.  
  226.     /* The real relocation info entry.
  227.        This is an ugly design.  In the original relocation_info structure.
  228.        The corresponding symbol definition is located by an index to the
  229.        nlist array.  This array is not kept online, and so this index
  230.        must be replace by the address of the corresponding symbol table
  231.        kept online.  However, on most machine this index takes only 24bits,
  232.        which is not large enough (in general) to hold a pointer.  So we
  233.        need a structure definition with a sp field as shown above.  The
  234.        complete original relocation info entry is kept here.  This is not
  235.        very space economical (24 bits are wasted).  But since different
  236.        system has different format for STRUCT RELOCATION_INFO, I'd rather
  237.        let the include file <a.out.h> take care of the difference, than
  238.        using a separate definition for each system. */
  239.     struct relocation_info reloc_info;
  240. };
  241.  
  242. /* Format of __.SYMDEF:
  243.    First, a longword containing the size of the 'symdef' data that follows.
  244.    Second, zero or more 'symdef' structures.
  245.    Third, a word containing the length of symbol name strings.
  246.    Fourth, zero or more symbol name strings (each followed by a zero).  */
  247.  
  248. struct symdef {
  249.   int symbol_name_string_index;
  250.   int library_member_offset;
  251. };
  252.  
  253. /* variable for saving the environment */
  254. jmp_buf _dld_env;
  255.  
  256. /* pointer to the lastest (newest) file entry */
  257. struct file_entry *_dld_latest_entry = 0;
  258.  
  259. /* dummy file_entry to hold all "dangling" symbols. */
  260. struct file_entry *_dld_dummy_entry = 0;
  261.  
  262. /* To avoid close a file and then open the same file again, the following
  263.    two variables remember the file that is currently open.  Both are zero
  264.    if no file is open.
  265.  */
  266. static struct file_entry *input_file = 0;
  267. static int input_desc = 0;
  268.  
  269. /* global variables to return the error code to the caller */
  270. int dld_errno;
  271.  
  272. /* true if the executable flags are up-to-date */
  273. char _dld_exec_flags_valid;
  274.  
  275. /* Miscellaneous routines */
  276.  
  277. /* save the error code in dld_errno */
  278. static void
  279. fatal (errno)
  280. register int errno;
  281. {
  282.     dld_errno = errno;
  283.     longjmp (_dld_env, 1);
  284. } /* fatal */
  285.  
  286.  
  287. /* Like malloc but get fatal error if memory is exhausted.  */
  288.  
  289. _dld_malloc (size)
  290. int size;
  291. {
  292.     register int result = malloc (size);
  293.     if (!result)
  294.     fatal (DLD_ENOMEMORY);
  295.     return result;
  296. } /* _dld_malloc */
  297.  
  298.  
  299. /* Return a newly-allocated string
  300.    whose contents concatenate the strings S1, S2, S3.  */
  301.  
  302. static char *
  303. concat (s1, s2, s3)
  304. char *s1, *s2, *s3;
  305. {
  306.     register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  307.     register char *result = (char *) _dld_malloc (len1 + len2 + len3 + 1);
  308.     
  309.     strcpy (result, s1);
  310.     strcpy (result + len1, s2);
  311.     strcpy (result + len1 + len2, s3);
  312.     result[len1 + len2 + len3] = 0;
  313.     
  314.     return result;
  315. } /* concat */
  316.  
  317.  
  318. /* Add a new entry to the file chain if it is not already there.
  319.    Return 0 if no actual insertion is needed, otherwise, return 1. */
  320. static
  321. insert_entry (head, entry)
  322. struct file_chain **head;
  323. register struct file_entry *entry;
  324. {
  325.     register struct file_chain *fc = *head;
  326.     
  327.     while (fc)  {
  328.         if (fc->entry == entry)
  329.             return 0;
  330.         else fc = fc->next;
  331.     }
  332.     
  333.     fc = (struct file_chain *) _dld_malloc (sizeof (struct file_chain));
  334.  
  335.     fc->next = *head;
  336.     fc->entry = entry;
  337.     *head = fc;
  338.     return 1;
  339. } /* insert_entry */
  340.  
  341.  
  342. /* Close the input file that is now open.  */
  343.  
  344. static void
  345. file_close ()
  346. {
  347.   if (input_desc) close (input_desc);
  348.   input_desc = 0;
  349.   input_file = 0;
  350. } /* file_close */
  351.  
  352. /* Open the input file specified by 'entry', and return a descriptor.
  353.    The open file is remembered; if the same file is opened twice in a row,
  354.    a new open is not actually done.  */
  355.  
  356. static int
  357. file_open (entry)
  358. register struct file_entry *entry;
  359. {
  360.     register int desc;
  361.     
  362.     if (entry == 0) fatal (DLD_ENOFILE);
  363.     
  364.     if (entry->superfile)
  365.     return file_open (entry->superfile);
  366.     
  367.     if (entry == input_file)
  368.     return input_desc;
  369.     
  370.     if (input_file) file_close ();
  371.     
  372.     desc = open (entry->filename, O_RDONLY, 0);
  373.     
  374.     if (desc > 0) {
  375.     input_file = entry;
  376.     input_desc = desc;
  377.     return desc;
  378.     }
  379.     
  380.     fatal (DLD_ENOFILE);
  381.     return 0; /* dummy return */
  382. } /* file_open */
  383.  
  384. /* Medium-level input routines for rel files.  */
  385.  
  386. /* Read a file's header into the proper place in the file_entry.
  387.    DESC is the descriptor on which the file is open.
  388.    ENTRY is the file's entry.  */
  389.  
  390. static void
  391. read_header (desc, entry)
  392. int desc;
  393. register struct file_entry *entry;
  394. {
  395.     register int len;
  396.     struct exec *loc = &entry->header;
  397.     
  398.     lseek (desc, entry->starting_offset, 0);
  399.     len = read (desc, loc, sizeof (struct exec));
  400.     if (len != sizeof (struct exec))
  401.     fatal (DLD_EBADHEADER);
  402.     if (N_BADMAG (*loc))
  403.     fatal (DLD_EBADMAGIC);
  404.     
  405.     entry->header_read_flag = 1;
  406. } /* read_header */
  407.  
  408. #ifdef atarist
  409. #include <st-out.h>
  410.  
  411. static void
  412. st_read_header (desc, entry)
  413. int desc;
  414. register struct file_entry *entry;
  415. {
  416.     register int len;
  417.     struct exec *loc = &entry->header;
  418.     struct aexec file_header;
  419.     
  420.     lseek (desc, 0L, 0);
  421.     len = read (desc, &file_header, sizeof (struct aexec));
  422.     if (len != sizeof (struct aexec))
  423.     fatal (DLD_EBADHEADER);
  424.     if (A_BADMAG (file_header))
  425.     fatal (DLD_EBADMAGIC);
  426.     if (!file_header.a_AZero2)
  427.     fatal (DLD_ENOSTRINGS);
  428.  
  429.     bzero (loc, sizeof (struct exec));
  430.     loc->a_magic = NMAGIC;    /* flag different header size */
  431.     loc->a_text = file_header.a_text;
  432.     loc->a_data = file_header.a_data;
  433.     loc->a_bss = file_header.a_bss;
  434.     loc->a_syms = file_header.a_syms - file_header.a_AZero2;
  435.     entry->header_read_flag = 1;
  436. } /* st_read_header */
  437. #endif /* atarist */
  438.  
  439. /* Read the symbols of file ENTRY into core.
  440.    Assume it is already open, on descriptor DESC.
  441.    Also read the length of the string table, which follows the symbol table,
  442.    but don't read the contents of the string table.  */
  443.  
  444. static void
  445. read_entry_symbols (desc, entry)
  446. struct file_entry *entry;
  447. int desc;
  448. {
  449.     int str_size;
  450.     
  451.     if (!entry->header_read_flag)
  452.     read_header (desc, entry);
  453.     
  454.     if (entry->header.a_syms <= 0)
  455.     fatal (DLD_ENOSYMBOLS);
  456.     
  457.     entry->symbols = (struct nlist *) _dld_malloc (entry->header.a_syms);
  458.     
  459.     lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  460.     if (entry->header.a_syms !=
  461.     read (desc, entry->symbols, entry->header.a_syms)) {
  462.     free (entry->symbols);
  463.     entry->symbols = 0;
  464.     fatal (DLD_ENOSYMBOLS);
  465.     }
  466.     
  467.     lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  468.     if (sizeof str_size != read (desc, &str_size, sizeof str_size)) {
  469.     free (entry->symbols);
  470.     entry->symbols = 0;
  471.     fatal (DLD_ENOSTRINGS);
  472.     }
  473.     
  474.     entry->string_size = str_size;
  475. } /* read_entry_symboles */
  476.  
  477.  
  478. /* Read the string table of file ENTRY into core.
  479.    Assume it is already open, on descriptor DESC. */
  480.  
  481. static void
  482. read_entry_strings (desc, entry)
  483. struct file_entry *entry;
  484. int desc;
  485. {
  486.     if (!entry->header_read_flag)
  487.     read_header (desc, entry);
  488.     
  489.     entry->strings = (char *) _dld_malloc (entry->string_size);
  490.  
  491.     lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  492.     if (entry->string_size != read (desc, entry->strings,
  493.                     entry->string_size)) {
  494.     free (entry->strings);
  495.     entry->strings = 0;
  496.     fatal (DLD_ENOSTRINGS);
  497.     }
  498.     
  499. } /* read_entry_strings */
  500.  
  501.  
  502. /* Verify the validity of the relocation information.
  503.    DATA_SIZE is the length of the contents.
  504.    RELOC_INFO is the address of the relocation info, in core.
  505.    RELOC_SIZE is its length in bytes.
  506.    If everything is ok, return the number of external relocation entry.
  507.    Otherwise, return -1. */
  508. reloc_info_ok (data_size, reloc_info, reloc_size, sym_size)
  509. register int data_size;
  510. register struct relocation_info *reloc_info;
  511. register int reloc_size;
  512. register int sym_size;
  513. {
  514.     register struct relocation_info *p = reloc_info;
  515.     register struct relocation_info *end = p +
  516.     reloc_size / sizeof (struct relocation_info);
  517.     register int extern_count = 0;  /* number of external relocation */
  518.     
  519.     for (; p < end; p++) {
  520.     register int symbolnum = RELOC_SYMBOL(p);
  521.     
  522.     if (RELOC_ADDRESS(p) >= data_size)
  523.         return -1;
  524.     if (RELOC_EXTERN_P(p)) {
  525.         extern_count++;
  526.         if (symbolnum * sizeof (struct nlist) >= sym_size)
  527.         return -1;
  528.     } else if (symbolnum != N_TEXT && symbolnum != (N_TEXT | N_EXT) &&
  529.            symbolnum != N_DATA && symbolnum != (N_DATA | N_EXT) &&
  530.            symbolnum != N_BSS  && symbolnum != (N_BSS | N_EXT))
  531.         return -1;
  532.  
  533.     if (RELOC_TARGET_SIZE(p) > 2) return -1;
  534.     }
  535.     return extern_count++;
  536.     
  537. } /* reloc_info_ok */
  538.  
  539. /* symbol table management */
  540.  
  541. /* Compute the hash code for symbol name KEY.  */
  542.  
  543. static int
  544. hash_string (key)
  545. char *key;
  546. {
  547.     register char *cp;
  548.     register int k;
  549.     
  550.     cp = key;
  551.     k = 0;
  552.     while (*cp)
  553.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  554.     
  555.     return k;
  556. } /* hash_string */
  557.  
  558.  
  559. /* Get the symbol table entry for the global symbol named KEY.
  560.    Create one if there is none.  */
  561.  
  562. symbol *
  563. _dld_getsym (key)
  564. char *key;
  565. {
  566.     register int hashval;
  567.     register symbol *bp;
  568.     
  569.     /* Determine the proper bucket.  */
  570.     
  571.     hashval = hash_string (key) % TABSIZE;
  572.     
  573.     /* Search the bucket.  */
  574.     
  575.     for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
  576.     if (! strcmp (key, bp->name))
  577.         return bp;
  578.     
  579.     /* Nothing was found; create a new symbol table entry.  */
  580.     
  581.     bp = (symbol *) _dld_malloc (sizeof (symbol));
  582.     bzero (bp, sizeof (symbol));
  583.     bp->name = (char *) _dld_malloc (strlen (key) + 1);
  584.     strcpy (bp->name, key);
  585.     
  586.     /* Add the entry to the bucket.  */
  587.     
  588.     bp->link = _dld_symtab[hashval];
  589.     _dld_symtab[hashval] = bp;
  590.     
  591.     return bp;
  592. } /* _dld_getsym */
  593.  
  594.  
  595. /* Like `_dld_getsym' but return 0 if the symbol is not already known.  */
  596.  
  597. symbol *
  598. _dld_getsym_soft (key)
  599. char *key;
  600. {
  601.     register int hashval;
  602.     register symbol *bp;
  603.     
  604.     /* Determine which bucket.  */
  605.     
  606.     hashval = hash_string (key) % TABSIZE;
  607.     
  608.     /* Search the bucket.  */
  609.     
  610.     for (bp = _dld_symtab[hashval]; bp; bp = bp->link)
  611.     if (! strcmp (key, bp->name))
  612.         return bp;
  613.     
  614.     return 0;
  615. } /* _dld_getsym_soft */
  616.  
  617. /* Enter one global symbol in the hash table.
  618.    NLIST_P points to the `struct nlist' read from the file
  619.    that describes the global symbol.  NAME is the symbol's name.
  620.    ENTRY is the file entry for the file the symbol comes from.
  621.  
  622.    The `struct nlist' is modified by making n_name point back to the
  623.    corresponding symbols.
  624.  
  625.    For the common definition, the symbol is considered defined by the
  626.    first entry that defines it.  The problem here is that uninitialized
  627.    global variables are all treated as common definitions.  Strictly
  628.    speacking, in a program there should be exactly one definition
  629.    (initialized or uninitialized) of any global variable and all other
  630.    files that reference that variable should declare it with the 'extern'
  631.    keyword.  However, most compiler allows the omission of the 'extern'
  632.    keyword, and let the linker map all these definitions to the same
  633.    location.  In other words, multiple definitions of the same symbol is
  634.    allowed.  Now, when one of the files that defines such symbol is to be
  635.    unlinked, should this symbol become undefined?  Or should it remain in
  636.    core?  The decision made is to treat the symbol defined by the first
  637.    file that defines it.  All subsequence definitions of the same symbol
  638.    will be treated as extern references.  However, if any of the
  639.    following definition defines the symbol as initialized global, it will
  640.    be considered multiple definition and be treated as an error.
  641.  
  642. */
  643. void
  644. _dld_enter_global_ref (entry, nlist_p, name)
  645. struct file_entry *entry;
  646. register struct nlist *nlist_p;
  647. char *name;
  648. {
  649.     register symbol *sp = _dld_getsym (name);
  650.     register int type = nlist_p->n_type;
  651.     register int common = (type == (N_UNDF | N_EXT) && nlist_p->n_value);
  652.     int oldref = sp->referenced;
  653.     int olddef = sp->defined;
  654.  
  655.     nlist_p->n_un.n_name = (char *) sp;
  656.     
  657.     sp->referenced = 1;
  658.  
  659.     /* common definition */
  660.     if (common && !olddef)
  661.     type = N_COMM | N_EXT;
  662.  
  663.     if (type != (N_UNDF | N_EXT)) {
  664.     /* definition of a symbol */
  665.     
  666.     if (olddef)
  667.         fatal (DLD_EMULTDEFS);
  668.     else {
  669.         sp->defined = type;
  670.         sp->value = common ? (long) _dld_malloc (nlist_p->n_value) :
  671.         nlist_p->n_value; 
  672.         if (common) bzero (sp->value, nlist_p->n_value);
  673.         sp->defined_by = entry;
  674.     }
  675.  
  676.     if (oldref && !olddef) {
  677.         register struct file_chain *p = sp->referenced_by;
  678.         register struct file_chain *prev = 0;
  679.         
  680.         dld_undefined_sym_count--;
  681.         
  682.         /* We do not set up references link to and from the dummy
  683.            entry, nor do we touch the ref_count of it.
  684.            Furthermore, if a symbol referenced by the dummy entry is
  685.            defined, we remove all connections between the dummy entry
  686.            and this symbol. */
  687.         /* If the dummy entry references anything other than a library
  688.            member, this reference is removed. */
  689.         while (p) {
  690.         if (entry != _dld_dummy_entry) {
  691.             if (p->entry == _dld_dummy_entry && entry->superfile)
  692.             entry->ref_count++;
  693.             else if (insert_entry (&(p->entry->refs), entry) &&
  694.                  insert_entry (&(entry->refs_by), p->entry) &&
  695.                  p->entry != entry)
  696.             entry->ref_count++;
  697.         }
  698.  
  699.         if (p->entry == _dld_dummy_entry) {
  700.  
  701.             /* we force this to be an entry directly linked-in by
  702.                the user (top-level module). */
  703.             entry->already_unlink = 0;
  704.             del_link_list_elt (sp->referenced_by, prev, p, next);
  705.             continue;
  706.         } else p->entry->undefined_symbol_count--;
  707.         
  708.         prev = p;
  709.         p = p->next;
  710.         }
  711.     }
  712.     } else {
  713.     /* this is just a reference */
  714.     if (sp->defined_by && sp->defined_by != _dld_dummy_entry)
  715.         if (entry != _dld_dummy_entry) {
  716.         if (insert_entry (&(entry->refs), sp->defined_by) &&
  717.             entry != sp->defined_by)
  718.             sp->defined_by->ref_count++;
  719.         } else if (sp->defined_by->superfile) {
  720.         sp->defined_by->ref_count++;
  721.         sp->defined_by->already_unlink = 0;
  722.         }
  723.     
  724.     if (entry != _dld_dummy_entry) {
  725.         if (insert_entry (&(sp->referenced_by), entry) && !sp->defined)
  726.         entry->undefined_symbol_count++;
  727.     } else {
  728.         if (!sp->defined)
  729.         insert_entry (&(sp->referenced_by), entry);
  730.     }
  731.     
  732.     if (!oldref) {
  733.         dld_undefined_sym_count++;
  734.         sp->defined = 0;
  735.         sp->value = 0;
  736.     }
  737.     }
  738. }
  739.  
  740.  
  741. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  742.  
  743. static void
  744. enter_file_symbols (entry)
  745. struct file_entry *entry;
  746. {
  747.     register struct nlist *p,
  748.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  749.     
  750.     for (p = entry->symbols; p < end; p++)
  751.     if (p->n_type & N_EXT)
  752.         _dld_enter_global_ref (entry, p, p->n_un.n_strx + entry->strings);
  753. }
  754.  
  755. /* remove all symbols that are no longer needed */
  756. static void
  757. cleanup_symtab ()
  758. {
  759.     register int i;
  760.  
  761.     for (i = 0; i < TABSIZE; i++) {
  762.     register symbol *sp = _dld_symtab[i];
  763.     register symbol *prev_sp = 0;
  764.  
  765.     while (sp) {
  766.         register struct file_chain *p = sp->referenced_by;
  767.         register struct file_chain *prev = 0;
  768.         register int obsolete = (sp->defined_by &&
  769.                      sp->defined_by->ref_count == 0);
  770.         
  771.         while (p)
  772.         if (p->entry->ref_count == 0) {
  773.             del_link_list_elt (sp->referenced_by, prev, p, next);
  774.         } else {
  775.             if (obsolete)
  776.             p->entry->undefined_symbol_count++;
  777.  
  778.             prev = p;
  779.             p = p->next;
  780.         }
  781.  
  782.         if (obsolete) {
  783.         if (sp->defined == (N_COMM | N_EXT) && sp->value)
  784.             free (sp->value);
  785.         if (sp->referenced_by == 0) {
  786.             free (sp->name);
  787.             del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
  788.             continue;
  789.         } else {
  790.             dld_undefined_sym_count++;
  791.             sp->defined = 0;
  792.             sp->defined_by = 0;
  793.             sp->value = 0;
  794.         }
  795.         }
  796.  
  797.         if (sp->defined == 0 && sp->referenced_by == 0) {
  798.         dld_undefined_sym_count--;
  799.         free (sp->name);
  800.         if (sp->defined == (N_COMM | N_EXT) && sp->value)
  801.             free (sp->value);
  802.         del_link_list_elt (_dld_symtab[i], prev_sp, sp, link);
  803.         continue;
  804.         }
  805.  
  806.         prev_sp = sp;
  807.         sp = sp->link;
  808.     }
  809.     }
  810. } /* cleanup_symtab */
  811.  
  812. /* Searching libraries */
  813.  
  814. static struct file_entry *decode_library_subfile ();
  815. static void linear_library (), symdef_library ();
  816. static int subfile_wanted_p ();
  817. static void read_text_and_data ();
  818.  
  819. /* Search the library ENTRY, already open on descriptor DESC.
  820.    This means deciding which library members to load,
  821.    making a chain of `struct file_entry' for those members,
  822.    and entering their global symbols in the hash table.  */
  823.  
  824. static void
  825. search_library (desc, entry)
  826. int desc;
  827. struct file_entry *entry;
  828. {
  829.     int member_length;
  830.     register char *name;
  831.     register struct file_entry *subentry;
  832.     
  833.     if (!dld_undefined_sym_count) return;
  834.     
  835.     /* Examine its first member, which starts SARMAG bytes in.  */
  836.     subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  837.     if (!subentry) return;
  838.     
  839.     name = subentry->filename;
  840.     free (subentry);
  841.     
  842.     /* Search via __.SYMDEF if that exists, else linearly.  */
  843.     
  844.     if (!strcmp (name, "__.SYMDEF"))
  845.     symdef_library (desc, entry, member_length);
  846.     else
  847.     linear_library (desc, entry);
  848.     
  849.     free (name);
  850. } /* search_library */
  851.  
  852. /* Construct and return a file_entry for a library member.
  853.    The library's file_entry is library_entry, and the library is open on DESC.
  854.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  855.    We store the length of the member into *LENGTH_LOC.  */
  856.  
  857. static struct file_entry *
  858. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  859. int desc;
  860. struct file_entry *library_entry;
  861. int subfile_offset;
  862. int *length_loc;
  863. {
  864.     int bytes_read;
  865.     register int namelen;
  866.     int member_length;
  867.     register char *name;
  868.     struct ar_hdr hdr1;
  869.     register struct file_entry *subentry;
  870.     
  871.     lseek (desc, subfile_offset, 0);
  872.     
  873.     bytes_read = read (desc, &hdr1, sizeof hdr1);
  874.     if (!bytes_read)
  875.     return 0;        /* end of archive */
  876.     
  877.     if (sizeof hdr1 != bytes_read)
  878.     fatal (DLD_EBADLIBRARY);
  879.     
  880.     if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  881.     fatal (DLD_EBADLIBRARY);
  882.     
  883.     subentry = (struct file_entry *) _dld_malloc (sizeof (struct file_entry));
  884.     bzero (subentry, sizeof (struct file_entry));
  885.     
  886.     for (namelen = 0;
  887.      namelen < sizeof hdr1.ar_name
  888.      && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ';
  889.      namelen++);
  890.     
  891.     name = (char *) _dld_malloc (namelen+1);
  892.     strncpy (name, hdr1.ar_name, namelen);
  893.     name[namelen] = 0;
  894.     
  895.     subentry->filename = name;
  896.     subentry->local_sym_name = name;
  897.     subentry->starting_offset = subfile_offset + sizeof hdr1;
  898.     subentry->superfile = library_entry;
  899.     subentry->total_size = member_length;
  900.     
  901.     (*length_loc) = member_length;
  902.     
  903.     return subentry;
  904. } /* decode_library_subfile */
  905.  
  906.  
  907. /* Search a library that has a __.SYMDEF member.
  908.    DESC is a descriptor on which the library is open.
  909.      The file pointer is assumed to point at the __.SYMDEF data.
  910.    ENTRY is the library's file_entry.
  911.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  912.  
  913. static void
  914. symdef_library (desc, entry, member_length)
  915. int desc;
  916. struct file_entry *entry;
  917. int member_length;
  918. {
  919.     int *symdef_data = (int *) _dld_malloc (member_length);
  920.     register struct symdef *symdef_base;
  921.     char *sym_name_base;
  922.     int number_of_symdefs;
  923.     int length_of_strings;
  924.     int not_finished;
  925.     int bytes_read;
  926.     register int i;
  927.     struct file_entry *prev = 0;
  928.     int prev_offset = 0;
  929.     
  930.     bytes_read = read (desc, symdef_data, member_length);
  931.     if (bytes_read != member_length) {
  932.     free (symdef_data);
  933.     fatal (DLD_EBADLIBRARY);
  934.     }
  935.  
  936.     /* Ugly kludge here:  save the address for symdef_data in
  937.        entry->strings so that if fatal() is called, this memory block will
  938.        be freed by clean_up().  If no error occurs, it is freed before this
  939.        function returns.  I use entry->strings just for convenience and
  940.        don't want to add an extra entry in struct file_entry.  
  941.        Originally storage for symdef_data was
  942.        allocated by alloca(), but I want to avoid the use of that. */
  943.  
  944.     entry->strings = (char *) symdef_data;
  945.     
  946.     number_of_symdefs = *symdef_data / sizeof (struct symdef);
  947.     if (number_of_symdefs < 0 ||
  948.     number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) >=
  949.     member_length)
  950.     fatal (DLD_EBADLIBRARY);
  951.     
  952.     symdef_base = (struct symdef *) (symdef_data + 1);
  953.     length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  954.     
  955.     if (length_of_strings < 0
  956.     || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  957.     + 2 * sizeof (int) != member_length)
  958.     fatal (DLD_EBADLIBRARY);
  959.     
  960.     sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  961.     
  962.     /* Check all the string indexes for validity.  */
  963.     
  964.     for (i = 0; i < number_of_symdefs; i++) {
  965.     register int index = symdef_base[i].symbol_name_string_index;
  966.     if (index < 0 || index >= length_of_strings
  967.         || (index && *(sym_name_base + index - 1)))
  968.         fatal (DLD_EBADLIBRARY);
  969.     }
  970.     
  971.     /* Search the symdef data for members to load.
  972.        Do this until one whole pass finds nothing to load.  */
  973.     
  974.     not_finished = 1;
  975.     while (not_finished) {
  976.     not_finished = 0;
  977.         
  978.     /* Scan all the symbols mentioned in the symdef for ones that we need.
  979.        Load the library members that contain such symbols.  */
  980.         
  981.     for (i = 0; i < number_of_symdefs && dld_undefined_sym_count; i++)
  982.         if (symdef_base[i].symbol_name_string_index >= 0) {
  983.         register symbol *sp;
  984.             
  985.         sp = _dld_getsym_soft (sym_name_base +
  986.                        symdef_base[i].symbol_name_string_index);
  987.             
  988.         /* If we find a symbol that appears to be needed,
  989.            think carefully about the archive member that
  990.            the symbol is in.  */
  991.             
  992.         if (sp && sp->referenced && !sp->defined) {
  993.             int junk;
  994.             register int j;
  995.             register int offset = symdef_base[i].library_member_offset;
  996.             struct file_entry *subentry;
  997.                 
  998.             /* Don't think carefully about any archive member
  999.                more than once in a given pass.  */
  1000.             
  1001.             if (prev_offset == offset)
  1002.             continue;
  1003.             prev_offset = offset;
  1004.             
  1005.             /* Read the symbol table of the archive member.  */
  1006.             
  1007.             subentry = decode_library_subfile (desc, entry, offset, &junk);
  1008.             if (prev)
  1009.             prev->chain = subentry;
  1010.             else entry->subfiles = subentry;
  1011.                 
  1012.             read_entry_symbols (desc, subentry);
  1013.             read_entry_strings (desc, subentry);
  1014.                 
  1015.             /* Now scan the symbol table and decide whether to load.  */
  1016.             if (!subfile_wanted_p (subentry)) {
  1017.             if (prev)
  1018.                 prev->chain = 0;
  1019.             else entry->subfiles = 0;
  1020.             free (subentry->filename);
  1021.             free (subentry->symbols);
  1022.             free (subentry->strings);
  1023.             free (subentry);
  1024.             } else {
  1025.             /* This member is needed; load it.
  1026.                Since we are loading something on this pass,
  1027.                we must make another pass through the symdef data.  */
  1028.             
  1029.             not_finished = 1;
  1030.                     
  1031.             /* all library members are considered unlinked
  1032.                already so that they will be garbage collected
  1033.                whenever no other modules reference them. */
  1034.             subentry->already_unlink = 1;
  1035.  
  1036.             enter_file_symbols (subentry);
  1037.             free (subentry->strings);
  1038.             subentry->strings = 0;
  1039.                     
  1040.             read_text_and_data (desc, subentry);
  1041.             
  1042.             prev = subentry;
  1043.                     
  1044.             /* Clear out this member's symbols from the symdef data
  1045.                so that following passes won't waste time on them.  */
  1046.                     
  1047.             for (j = 0; j < number_of_symdefs; j++) {
  1048.                 if (symdef_base[j].library_member_offset == offset)
  1049.                 symdef_base[j].symbol_name_string_index = -1;
  1050.             }
  1051.             }
  1052.         }
  1053.         }
  1054.     }
  1055.  
  1056.     /* refer to "Ugly kludge" above. */
  1057.     free (entry->strings);
  1058.     entry->strings = 0;
  1059. } /* symdef_library */ 
  1060.  
  1061.  
  1062. /* Search a library that has no __.SYMDEF.
  1063.    ENTRY is the library's file_entry.
  1064.    DESC is the descriptor it is open on.  */
  1065.  
  1066. static void
  1067. linear_library (desc, entry)
  1068. int desc;
  1069. struct file_entry *entry;
  1070. {
  1071.     register struct file_entry *prev = 0;
  1072.     register int this_subfile_offset = SARMAG;
  1073.     
  1074.     while (dld_undefined_sym_count) {
  1075.     int member_length;
  1076.     register struct file_entry *subentry;
  1077.         
  1078.     subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  1079.         
  1080.     if (!subentry) return;
  1081.         
  1082.     read_entry_symbols (desc, subentry);
  1083.     read_entry_strings (desc, subentry);
  1084.         
  1085.     if (!subfile_wanted_p (subentry)) {
  1086.         free (subentry->filename);
  1087.         free (subentry->symbols);
  1088.         free (subentry->strings);
  1089.         free (subentry);
  1090.     } else {
  1091.         subentry->already_unlink = 1;
  1092.         enter_file_symbols (subentry);
  1093.         free (subentry->strings);
  1094.         subentry->strings = 0;
  1095.             
  1096.         read_text_and_data (desc, subentry);
  1097.         
  1098.         if (prev)
  1099.         prev->chain = subentry;
  1100.         else entry->subfiles = subentry;
  1101.         prev = subentry;
  1102.     }
  1103.         
  1104.     this_subfile_offset += member_length + sizeof (struct ar_hdr);
  1105.     if (this_subfile_offset & 1) this_subfile_offset++;
  1106.     }
  1107. } /* linear_library */
  1108.  
  1109.  
  1110. /* ENTRY is an entry for a library member.
  1111.    Its symbols have been read into core, but not entered.
  1112.    Return nonzero if we ought to load this member.  */
  1113.  
  1114. static int
  1115. subfile_wanted_p (entry)
  1116. struct file_entry *entry;
  1117. {
  1118.     register struct nlist *p;
  1119.     register struct nlist *end
  1120.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1121.     
  1122.     for (p = entry->symbols; p < end; p++) {
  1123.     register int type = p->n_type;
  1124.         
  1125.     if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value)) {
  1126.         register char *name = p->n_un.n_strx + entry->strings;
  1127.         register symbol *sp = _dld_getsym_soft (name);
  1128.             
  1129.         /* If this symbol has not been hashed, we can't be looking
  1130.            for it. */
  1131.             
  1132.         if (!sp) continue;
  1133.             
  1134.         /* We are looking for definition of external symbols or common
  1135.            blocks that have not been defined but have been referenced */
  1136.         
  1137.         if (sp->referenced && !sp->defined &&
  1138.         (type != (N_UNDF | N_EXT) || p->n_value))
  1139.         return 1;
  1140.     }
  1141.     }
  1142.     
  1143.     return 0;
  1144. } /* subfile_wanted_p */
  1145.  
  1146. /* Relocate the addresses of the file's symbols.  */
  1147. static void
  1148. relocate_symbol_address (entry)
  1149. register struct file_entry *entry;
  1150. {
  1151.     register struct nlist *p;
  1152.     register struct nlist *end
  1153.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1154.     register int text_relocation, data_relocation, bss_relocation;
  1155.  
  1156.     text_relocation = entry->text_start_address;
  1157.     data_relocation = entry->data_start_address - entry->header.a_text;
  1158.     bss_relocation = entry->bss_start_address - entry->header.a_text -
  1159.     entry->header.a_data;
  1160.     
  1161.     for (p = entry->symbols; p < end; p++) {
  1162.     /* If this belongs to a section,
  1163.        update it by the section's start address */
  1164.         
  1165.     register int type = p->n_type;
  1166.     register symbol *sp;
  1167.     
  1168.     if ((type & N_EXT) == 0) continue;
  1169.     
  1170.     sp = (symbol *) p->n_un.n_name;
  1171.     if (type == (N_TEXT | N_EXT))
  1172.         sp->value += text_relocation;
  1173.     else if (type == (N_DATA | N_EXT))
  1174.         /* A symbol whose value is in the data section
  1175.            is present in the input file as if the data section
  1176.            started at an address equal to the length of the
  1177.            file's text.  */
  1178.         sp->value += data_relocation;
  1179.     else if (type == (N_BSS | N_EXT))
  1180.         /* likewise for symbols with value in BSS.  */
  1181.         sp->value += bss_relocation;
  1182.     }
  1183. } /* relocate_symbol_address */
  1184.  
  1185.  
  1186. /* Actually performs the relocation of local symbols.
  1187.    Do it once and for all for each entry.
  1188.    Then keep only those with external references online.
  1189.    This function is copied almost directly from perform_relocation (). */
  1190. static void
  1191. do_local_relocation (data, pc_relocation, reloc_info, dld_reloc_p, reloc_size,
  1192.              entry)
  1193. char *data;
  1194. struct relocation_info *reloc_info;
  1195. struct dld_reloc_info *dld_reloc_p;
  1196. struct file_entry *entry;
  1197. int pc_relocation;
  1198. int reloc_size;
  1199. {
  1200.     register struct relocation_info *p = reloc_info;
  1201.  
  1202.     struct relocation_info *end
  1203.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  1204.  
  1205.     int text_relocation = entry->text_start_address;
  1206.     int data_relocation = entry->data_start_address - entry->header.a_text;
  1207.     int bss_relocation
  1208.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  1209.     
  1210.     for (; p < end; p++) {
  1211.     register int relocation = 0;
  1212.     register int addr = RELOC_ADDRESS(p);
  1213.     register int symbolnum = RELOC_SYMBOL(p);
  1214.  
  1215. #if defined(sun) && defined(sparc)
  1216.     register unsigned int mask = 0;
  1217. #else
  1218.     register int length = RELOC_TARGET_SIZE(p);
  1219. #endif    
  1220.         
  1221.     if (RELOC_EXTERN_P(p)) {
  1222.         register int symindex = symbolnum * sizeof (struct nlist);
  1223.  
  1224.         dld_reloc_p->sp = ((symbol *)
  1225.                    (((struct nlist *)
  1226.                  (((char *)entry->symbols) + symindex))
  1227.                 ->n_un.n_name));
  1228.         bcopy (p, &(dld_reloc_p->reloc_info),
  1229.            sizeof (struct relocation_info));
  1230.         dld_reloc_p++;
  1231.         continue;
  1232.     } else switch (symbolnum) {
  1233.         case N_TEXT:
  1234.         case N_TEXT | N_EXT:
  1235.             relocation = text_relocation;
  1236.         break;
  1237.             
  1238.         case N_DATA:
  1239.         case N_DATA | N_EXT:
  1240.         /* A word that points to beginning of the the data section
  1241.            initially contains not 0 but rather the "address" of
  1242.            that section in the input file, which is the length of
  1243.            the file's text.  */
  1244.         relocation = data_relocation;
  1245.         break;
  1246.             
  1247.         case N_BSS:
  1248.         case N_BSS | N_EXT:
  1249.         /* Similarly, an input word pointing to the beginning of
  1250.            the bss initially contains the length of text plus data
  1251.            of the file.  */
  1252.         relocation = bss_relocation;
  1253.         break;
  1254.             
  1255.         case N_ABS:
  1256.         case N_ABS | N_EXT:
  1257.         /* just in case */
  1258.         break;
  1259.         
  1260.         default:
  1261.         break;
  1262.     }
  1263.  
  1264.     if (RELOC_PCREL_P(p))
  1265.         relocation -= pc_relocation;
  1266.  
  1267. #if defined(sun) && defined(sparc)
  1268.  
  1269.     relocation += RELOC_ADD_EXTRA(p);
  1270.  
  1271.     relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  1272.  
  1273.     /* Unshifted mask for relocation */
  1274.     mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  1275.     mask |= mask - 1;
  1276.     relocation &= mask;
  1277.  
  1278.     switch (RELOC_TARGET_SIZE(p)) {
  1279.         case 0:
  1280.             *(char *) (data + addr) &= ~mask;
  1281.         *(char *) (data + addr) |= relocation;
  1282.         break;
  1283.             
  1284.         case 1:
  1285.             *(short *) (data + addr) &= ~mask;
  1286.         *(short *) (data + addr) |= relocation;
  1287.         break;
  1288.             
  1289.         case 2:
  1290.             *(long *) (data + addr) &= ~mask;
  1291.         *(long *) (data + addr) |= relocation;
  1292.         break;
  1293.  
  1294.         default: break;
  1295.     }
  1296.  
  1297. #else    
  1298.     
  1299.     switch (length) {
  1300.         case 0: *(char *) (data + addr) += relocation;
  1301.             break;
  1302.             
  1303.         case 1: *(short *) (data + addr) += relocation;
  1304.             break;
  1305.             
  1306.         case 2: *(int *) (data + addr) += relocation;
  1307.             break;
  1308.  
  1309.         default: break;
  1310.     }
  1311. #endif
  1312.     }
  1313. } /* do_local_relocation */
  1314.  
  1315.  
  1316. /* Read the relocation information of file ENTRY into core.
  1317.    Assume it is already open, on descriptor DESC.
  1318.    Then relocate all the local (non-external) symbols.  Save only the
  1319.    relocation info for the external symbol references.
  1320.    Update entry->header.a_{trsize,drsize} to reflect the new relocation
  1321.    table size (in bytes).
  1322. */
  1323. static void
  1324. relocate_local_refs (desc, entry)
  1325. int desc;
  1326. struct file_entry *entry;
  1327. {
  1328.     int text_offset;
  1329.  
  1330.     /* number of relocation info that describes an external references. */
  1331.     int tr_entry_count = 0, dr_entry_count = 0;    
  1332.                      
  1333.     struct relocation_info *reloc_buf;
  1334.  
  1335.     if (!entry->header_read_flag)
  1336.     read_header (desc, entry);
  1337.  
  1338.     text_offset = entry->starting_offset + N_TXTOFF (entry->header);
  1339.  
  1340.     /* For the text segment */
  1341.     
  1342.     if (entry->header.a_trsize) {
  1343.     reloc_buf = (struct relocation_info *)
  1344.         _dld_malloc (entry->header.a_trsize);
  1345.  
  1346.     lseek (desc, text_offset + entry->header.a_text +
  1347.            entry->header.a_data, 0);
  1348.     if (entry->header.a_trsize !=
  1349.         read (desc, reloc_buf, entry->header.a_trsize)) {
  1350.         free (reloc_buf);
  1351.         fatal (DLD_ENOTXTRELOC);
  1352.     }
  1353.     if ((tr_entry_count =
  1354.          reloc_info_ok (entry->header.a_text, reloc_buf,
  1355.                 entry->header.a_trsize, entry->header.a_syms)
  1356.          ) == -1) {
  1357.         free (reloc_buf);
  1358.         fatal (DLD_EBADRELOC);
  1359.     } else {
  1360.         entry->text_reloc = (struct dld_reloc_info *)
  1361.         _dld_malloc (tr_entry_count * sizeof (struct dld_reloc_info));
  1362.         do_local_relocation (entry->text_start_address,
  1363.                  entry->text_start_address,
  1364.                  reloc_buf, entry->text_reloc,
  1365.                  entry->header.a_trsize, entry);
  1366.         free (reloc_buf);
  1367.     }
  1368.     }
  1369.  
  1370.     /* For the data segment */
  1371.     
  1372.     if (entry->header.a_drsize) {
  1373.     reloc_buf = (struct relocation_info *)
  1374.         _dld_malloc (entry->header.a_drsize);
  1375.  
  1376.     lseek (desc, text_offset + entry->header.a_text +
  1377.            entry->header.a_data + entry->header.a_trsize, 0);
  1378.     if (entry->header.a_drsize !=
  1379.         read (desc, reloc_buf, entry->header.a_drsize)) {
  1380.         free (reloc_buf);
  1381.         fatal (DLD_ENODATRELOC);
  1382.     }
  1383.     if ((dr_entry_count =
  1384.          reloc_info_ok (entry->header.a_data, reloc_buf,
  1385.                 entry->header.a_drsize, entry->header.a_syms)
  1386.          ) == -1) {
  1387.         free (reloc_buf);
  1388.         fatal (DLD_EBADRELOC);
  1389.     } else {
  1390.         entry->data_reloc = (struct dld_reloc_info *)
  1391.         _dld_malloc (dr_entry_count * sizeof (struct dld_reloc_info));
  1392.         do_local_relocation (entry->data_start_address,
  1393.                  entry->data_start_address -
  1394.                  entry->header.a_data, 
  1395.                  reloc_buf, entry->data_reloc,
  1396.                  entry->header.a_drsize, entry);
  1397.         free (reloc_buf);
  1398.     }
  1399.     }
  1400.  
  1401.     entry->header.a_trsize = tr_entry_count * sizeof (struct dld_reloc_info);
  1402.     entry->header.a_drsize = dr_entry_count * sizeof (struct dld_reloc_info);
  1403.  
  1404.     /* Free the nlist array. */
  1405.     if (entry->symbols) {
  1406.     free (entry->symbols);
  1407.     entry->symbols = 0;
  1408.     }
  1409.     
  1410. } /* relocate_local_refs */
  1411.  
  1412.  
  1413. /* Relocate ENTRY's text or data section contents.
  1414.    DATA is the address of the contents, in core.
  1415.    DATA_SIZE is the length of the contents.
  1416.    PC_RELOCATION is the difference between the address of the contents
  1417.      in the output file and its address in the input file.
  1418.    RELOC_INFO is the address of the relocation info, in core.
  1419.    RELOC_SIZE is its length in bytes.
  1420.    REVERSE is true when an 'un-relocation' is to be done.
  1421. */   
  1422.  
  1423. static void
  1424. perform_relocation (data, pc_relocation, reloc_info, reloc_size, reverse)
  1425. char *data;
  1426. struct dld_reloc_info *reloc_info;
  1427. int pc_relocation;
  1428. int reloc_size;
  1429. int reverse;
  1430. {
  1431.     register struct dld_reloc_info *p = reloc_info;
  1432.  
  1433.     struct dld_reloc_info *end
  1434.     = reloc_info + reloc_size / sizeof (struct dld_reloc_info);
  1435.     
  1436. #if defined(sun) && defined(sparc)
  1437.     if (reverse) return;
  1438. #endif
  1439.     
  1440.     for (; p < end; p++) {
  1441.     register int relocation = p->sp->value;
  1442.     register struct relocation_info *r = &(p->reloc_info);
  1443.     register int addr = RELOC_ADDRESS(r);
  1444.     
  1445. #if defined(sun) && defined(sparc)
  1446.     register unsigned int mask = 0;
  1447. #else
  1448.     register int length = RELOC_TARGET_SIZE(r);
  1449. #endif    
  1450.         
  1451.     if (RELOC_PCREL_P(r))
  1452.         relocation -= pc_relocation;
  1453.  
  1454. #if defined(sun) && defined(sparc)
  1455.  
  1456.     relocation += RELOC_ADD_EXTRA(r);
  1457.  
  1458.     relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
  1459.  
  1460.     /* Unshifted mask for relocation */
  1461.     mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
  1462.     mask |= mask - 1;
  1463.     relocation &= mask;
  1464.  
  1465.     switch (RELOC_TARGET_SIZE(r)) {
  1466.         case 0:
  1467.             *(char *) (data + addr) &= ~mask;
  1468.         *(char *) (data + addr) |= relocation;
  1469.         break;
  1470.             
  1471.         case 1:
  1472.             *(short *) (data + addr) &= ~mask;
  1473.         *(short *) (data + addr) |= relocation;
  1474.         break;
  1475.             
  1476.         case 2:
  1477.             *(long *) (data + addr) &= ~mask;
  1478.         *(long *) (data + addr) |= relocation;
  1479.         break;
  1480.  
  1481.         default: break;
  1482.     }
  1483.  
  1484. #else    
  1485.     
  1486.     if (reverse) relocation = - relocation;
  1487.         
  1488.     switch (length) {
  1489.       case 0:
  1490.         if (RELOC_MEMORY_SUB_P(r))
  1491.         *(char *) (data + addr) = relocation - *(char *) (data + addr);
  1492.         else *(char *) (data + addr) += relocation;
  1493.         break;
  1494.             
  1495.       case 1:
  1496.         if (RELOC_MEMORY_SUB_P(r))
  1497.         *(short *) (data + addr) =
  1498.             relocation - *(short *) (data + addr);
  1499.         else *(char *) (data + addr) += relocation;
  1500.         break;
  1501.             
  1502.       case 2: 
  1503.         if (RELOC_MEMORY_SUB_P(r))
  1504.         *(int *) (data + addr) = relocation - *(int *) (data + addr);
  1505.         else *(int *) (data + addr) += relocation;
  1506.         break;
  1507.       default: break;
  1508.     }
  1509. #endif
  1510.     }
  1511. } /* perform_relocation */
  1512.  
  1513. /* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
  1514. #ifdef atarist
  1515. #define ABSOLUTE_FILENAME_P(fname)    ((fname[0] == '/') || \
  1516.     (fname[0] && (fname[1] == ':')))
  1517. #else
  1518. #define ABSOLUTE_FILENAME_P(fname)    (fname[0] == '/')
  1519. #endif /* atarist */
  1520.  
  1521. /* given a file name, create an appropriate file_entry for it */
  1522. static struct file_entry *
  1523. make_entry (filename)
  1524. char *filename;
  1525. {
  1526.     register struct file_entry *entry =
  1527.     (struct file_entry *) _dld_malloc (sizeof (struct file_entry));
  1528.  
  1529.     bzero (entry, sizeof (struct file_entry));
  1530.     entry->filename = entry->local_sym_name =
  1531.     (char *) _dld_malloc (strlen (filename) + 1);
  1532.     strcpy (entry->local_sym_name, filename);
  1533.     
  1534.     if (!ABSOLUTE_FILENAME_P(filename)) {
  1535.     char name[MAXPATHLEN];
  1536.     entry->filename = concat (getwd(name), "/", filename);
  1537.     }
  1538.     entry->chain = _dld_latest_entry;
  1539.     entry->ref_count = 1;
  1540.     return entry;
  1541. } /* make_entry */
  1542.  
  1543.  
  1544. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1545.    If it is a library, search it and load the appropriate members
  1546.    (which means calling this function recursively on those members).  */
  1547.  
  1548. static void
  1549. read_file_symbols (desc, entry, load_text)
  1550. register int desc;
  1551. register struct file_entry *entry;
  1552. int load_text;                /* used only by dld_init */
  1553. {
  1554.     register int len;
  1555.     int magicnum;
  1556.     
  1557.     len = read (desc, &magicnum, sizeof magicnum);
  1558.     if (len != sizeof magicnum)
  1559.     fatal (DLD_EBADHEADER);
  1560.     
  1561. #ifdef atarist
  1562.     if (!load_text || !N_BADMAG (*((struct exec *)&magicnum))) {
  1563. #else
  1564.     if (!N_BADMAG (*((struct exec *)&magicnum))) {
  1565. #endif
  1566.     read_entry_symbols (desc, entry);
  1567.     read_entry_strings (desc, entry);
  1568.     enter_file_symbols (entry);
  1569.     free (entry->strings);
  1570.     entry->strings = 0;
  1571.     if (load_text) read_text_and_data (desc, entry);
  1572.     } else {
  1573.     char armag[SARMAG];
  1574.     
  1575.     lseek (desc, 0, 0);
  1576.     if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1577.         fatal (DLD_EBADOBJECT);
  1578.     entry->library_flag = 1;
  1579.     search_library (desc, entry);
  1580.     }
  1581. } /* read_file_symbols */
  1582.  
  1583.  
  1584. /* Allocate memory for all text, data and bss segments and read them in
  1585.    from the file. */
  1586. static void
  1587. read_text_and_data (desc, entry)
  1588. int desc;
  1589. register struct file_entry *entry;
  1590. {
  1591.     register size = entry->header.a_text + entry->header.a_data +
  1592.     entry->header.a_bss;
  1593.  
  1594.     entry->text_start_address = _dld_malloc (size);
  1595.     if (size - entry->header.a_text > 0) {
  1596.     entry->data_start_address = entry->text_start_address +
  1597.         entry->header.a_text;
  1598.     entry->bss_start_address = entry->data_start_address +
  1599.         entry->header.a_data;
  1600.     } else entry->data_start_address = entry->bss_start_address = 0;
  1601.     
  1602.  
  1603.     /* Read text and data sections into core.
  1604.        Note that the bss segment does not actually take up space in the
  1605.        object file, so its size must be subtracted from SIZE */
  1606.     
  1607.     lseek (desc, entry->starting_offset + N_TXTOFF(entry->header), 0);
  1608.     size -= entry->header.a_bss;
  1609.     if (size != read (desc, entry->text_start_address, size)) {
  1610.     free (entry->text_start_address);
  1611.     entry->text_start_address = entry->data_start_address =
  1612.         entry->bss_start_address = 0;
  1613.     fatal (DLD_ENODATA);
  1614.     }
  1615.  
  1616.     /* zero the bss segment */
  1617.     if (entry->header.a_bss)
  1618.     bzero ((void *) entry->bss_start_address, entry->header.a_bss);
  1619. } /* read_text_and_data */
  1620.     
  1621.  
  1622. /* Allocate memory for the text and data segments and relocate all local
  1623.    symbols */
  1624. static void
  1625. relocate_entry_symbols (desc, entry)
  1626. int desc;
  1627. register struct file_entry *entry;
  1628. {
  1629.     /* Compute start addresses of each sections and symbols.  */
  1630.     
  1631.     if (entry->library_flag) {
  1632.     register struct file_entry *subentry = entry->subfiles;
  1633.     for (; subentry; subentry = subentry->chain) {
  1634.         relocate_symbol_address (subentry);
  1635.         relocate_local_refs (desc, subentry);
  1636.     }
  1637.     } else {
  1638.     relocate_symbol_address (entry);
  1639.     relocate_local_refs (desc, entry);
  1640.     }
  1641. } /* relocate_entry_symbols */
  1642.     
  1643.  
  1644. /*  Find all modules have all external references defined but not resolved. */
  1645. void
  1646. _dld_patch_all_files (entry)
  1647. register struct file_entry *entry;
  1648. {
  1649.     while (entry) {
  1650.     if (entry->library_flag)
  1651.         _dld_patch_all_files (entry->subfiles);
  1652.     else if (!entry->all_symbols_resolved_flag &&
  1653.          entry->undefined_symbol_count == 0) {
  1654.         /* entry whose global references have just been resolved */
  1655.         perform_relocation (entry->text_start_address,
  1656.                 entry->text_start_address,
  1657.                 entry->text_reloc,
  1658.                 entry->header.a_trsize, 0);
  1659.         perform_relocation (entry->data_start_address,
  1660.                 entry->data_start_address -
  1661.                 entry->header.a_data,
  1662.                 entry->data_reloc,
  1663.                 entry->header.a_drsize, 0);
  1664.         entry->all_symbols_resolved_flag = 1;
  1665.     } else if (entry->all_symbols_resolved_flag &&
  1666.            entry->undefined_symbol_count != 0) {
  1667.         /* entry that has some of its global references being
  1668.            "un-defined" */
  1669.         perform_relocation (entry->text_start_address,
  1670.                 entry->text_start_address,
  1671.                 entry->text_reloc,
  1672.                 entry->header.a_trsize, 1);
  1673.         perform_relocation (entry->data_start_address,
  1674.                 entry->data_start_address -
  1675.                 entry->header.a_data,
  1676.                 entry->data_reloc,
  1677.                 entry->header.a_drsize, 1);
  1678.         entry->all_symbols_resolved_flag = 0;
  1679.     }
  1680.  
  1681.     entry = entry->chain;
  1682.     }
  1683. } /* _dld_patch_all_files */
  1684.  
  1685.  
  1686. /*
  1687.  * reset the executable_flag of the given entry, and then recursively
  1688.  * propagate this to all modules that reference symbols in this entry.
  1689.  */
  1690. static void
  1691. invalidate (entry)
  1692. struct file_entry *entry;
  1693. {
  1694.     register struct file_chain *p;
  1695.     
  1696.     if (entry == 0)
  1697.     return;
  1698.     
  1699.     entry->executable_flag = 0;
  1700.  
  1701.     for (p = entry->refs_by; p; p = p->next)
  1702.     if (p->entry->executable_flag)
  1703.         invalidate (p->entry);
  1704. } /* invalidate */
  1705.  
  1706. /*
  1707.  * For all modules loaded, determine which of them can be safely executed.
  1708.  * For those that can, set the flag executable_flag.
  1709.  */
  1710. static void
  1711. find_all_executable_modules ()
  1712. {
  1713.     register struct file_entry *p;
  1714.  
  1715.     /* set all executable flags */
  1716.     for (p = _dld_latest_entry; p ; p = p->chain) {
  1717.     if (p->library_flag) {
  1718.         register struct file_entry *q = p->subfiles;
  1719.         while (q) {
  1720.         q->executable_flag = q->all_symbols_resolved_flag;
  1721.         q = q->chain;
  1722.         }
  1723.     }
  1724.     p->executable_flag = p->all_symbols_resolved_flag;
  1725.     }
  1726.     
  1727.     /* invalidate those modules that are not (yet) executable. */
  1728.     for (p = _dld_latest_entry; p; p = p->chain) {
  1729.     if (p->library_flag) {
  1730.         register struct file_entry *q = p->subfiles;
  1731.         while (q) {
  1732.         if (!q->all_symbols_resolved_flag && q->refs_by)
  1733.             invalidate (q);
  1734.         q = q->chain;
  1735.         }
  1736.     } else if (!p->all_symbols_resolved_flag && p->refs_by)
  1737.         invalidate (p);
  1738.     }
  1739.     _dld_exec_flags_valid = 1;
  1740. } /* find_all_executable_modules */
  1741.  
  1742.  
  1743. /* remove all reference pointers *TO* ENTRY */
  1744. static void 
  1745. remove_cross_references (head_of_chain, entry)
  1746. struct file_entry *head_of_chain, *entry;
  1747. {
  1748.     register struct file_entry *ep = head_of_chain;
  1749.     
  1750.     while (ep) {
  1751.     register struct file_chain *p = ep->refs;
  1752.     
  1753.     if (ep->library_flag)
  1754.         remove_cross_references (ep->subfiles, entry);
  1755.     else {
  1756.         register struct file_chain *prev = 0;
  1757.         
  1758.         while (p) {
  1759.         if (p->entry == entry || p->entry->superfile == entry) {
  1760.             del_link_list_elt (ep->refs, prev, p, next);
  1761.         } else {
  1762.             prev = p;
  1763.             p = p->next;
  1764.         }
  1765.         }
  1766.     }
  1767.     ep = ep->chain;
  1768.     }
  1769. } /* remove_cross_references */
  1770.  
  1771.  
  1772. /* remove all cross reference pointers related to an obsolete file entry.
  1773.    It is assumed that the ref_count of this entry is zero.
  1774.    Also clear up all other entries that become obsolete when this entry is
  1775.    removed. */
  1776. static void
  1777. cleanup_obsolete_entries (entry)
  1778. struct file_entry *entry;
  1779. {
  1780.     register struct file_chain *p;
  1781.  
  1782.     if (entry->ref_count != 0) return;
  1783.  
  1784.     if (entry->library_flag) {
  1785.     register struct file_entry *subentry = entry->subfiles;
  1786.  
  1787.     for (; subentry; subentry = subentry->chain)
  1788.         cleanup_obsolete_entries (subentry);
  1789.     }
  1790.  
  1791.     p = entry->refs;
  1792.     entry->refs = 0;            /* to prevent loop; might be redundant*/
  1793.  
  1794.     while (p) {
  1795.     register struct file_chain *next = p->next;
  1796.     
  1797.     if (p->entry->ref_count)    /* ref_count may already be set to zero */
  1798.         (p->entry->ref_count)--;
  1799.     if (p->entry->ref_count == 0)
  1800.         cleanup_obsolete_entries (p->entry);
  1801.     free (p);
  1802.     p = next;
  1803.     }
  1804. } /* cleanup_obsolete_entries */
  1805.  
  1806.  
  1807. /* remove all memory blocks assigned for ENTRY,
  1808.    return the pointers to the next entry in chain */
  1809. static struct file_entry *
  1810. kill_entry (entry)
  1811. register struct file_entry *entry;
  1812. {
  1813.     register struct file_chain *p;
  1814.  
  1815.     if (entry->library_flag) {
  1816.     register struct file_entry *subentry = entry->subfiles;
  1817.     while (subentry)
  1818.         subentry = kill_entry (subentry);
  1819.     }
  1820.     
  1821.     p = entry->refs;
  1822.     while (p) {
  1823.     register struct file_chain *next_chain = p->next;
  1824.     free (p);
  1825.     p = next_chain;
  1826.     }
  1827.  
  1828.     p = entry->refs_by;
  1829.     while (p) {
  1830.     register struct file_chain *next_chain = p->next;
  1831.     free (p);
  1832.     p = next_chain;
  1833.     }
  1834.  
  1835.     if (entry->local_sym_name) free (entry->local_sym_name);
  1836.  
  1837.     if (entry->filename != entry->local_sym_name && entry->filename)
  1838.     free (entry->filename);
  1839.  
  1840.     if (entry->symbols) free (entry->symbols);
  1841.  
  1842.     if (entry->strings) free (entry->strings);
  1843.     
  1844.     if (entry->text_reloc) free (entry->text_reloc);
  1845.  
  1846.     if (entry->data_reloc) free (entry->data_reloc);
  1847.  
  1848.     if (entry->text_start_address) free (entry->text_start_address);
  1849.  
  1850.     {
  1851.     register struct file_entry *next = entry->chain;
  1852.     free (entry);
  1853.     return (next);
  1854.     }
  1855. } /* kill_entry */
  1856.  
  1857.  
  1858. /* clean all data structures so that they return to the original states
  1859.    after last call to dld */
  1860. static void
  1861. clean_up ()
  1862. {
  1863.  
  1864.     if (!_dld_latest_entry)
  1865.     return;
  1866.  
  1867.     _dld_latest_entry->ref_count = 0;
  1868.     if (_dld_latest_entry->library_flag) {
  1869.     register struct file_entry *subentry = _dld_latest_entry->subfiles;
  1870.     for (; subentry; subentry = subentry->chain)
  1871.         subentry->ref_count = 0;
  1872.     }
  1873.     
  1874.     remove_cross_references (_dld_latest_entry, _dld_latest_entry);
  1875.  
  1876.     cleanup_obsolete_entries (_dld_latest_entry);
  1877.  
  1878.     cleanup_symtab ();
  1879.     
  1880.     _dld_latest_entry = kill_entry (_dld_latest_entry);
  1881. } /* clean_up */
  1882.  
  1883.  
  1884. /* Actually perform the unlink operation.
  1885.    Search through the list of file entries, unlink those whose ref_count is
  1886.    zero. */
  1887. static struct file_entry *
  1888. do_unlink (entry)
  1889. struct file_entry *entry;
  1890. {
  1891.     register struct file_entry *p = entry;
  1892.     register struct file_entry *prev = 0;
  1893.     
  1894.     if (p == 0) return p;
  1895.  
  1896.     while (p) {
  1897.     if (p->library_flag) {
  1898.         p->subfiles = do_unlink (p->subfiles);
  1899.  
  1900.         /* if all subentries are gone, remove itself */
  1901.         if (p->subfiles == 0)
  1902.         p->ref_count = 0;
  1903.     }
  1904.  
  1905.     if (p->ref_count == 0) {
  1906.         register struct file_entry *next;
  1907.         
  1908.         next = kill_entry (p);
  1909.     
  1910.         if (prev == 0) {
  1911.         entry = next;
  1912.         p = entry;
  1913.         } else {
  1914.         prev->chain = next;
  1915.         p = prev->chain;
  1916.         }
  1917.     } else {
  1918.         prev = p;
  1919.         p = p->chain;
  1920.     }
  1921.     }
  1922.     _dld_exec_flags_valid = 0;
  1923.     return entry;
  1924. } /* do_unlink */
  1925.  
  1926. dld_init (myname)
  1927. char *myname;
  1928. {
  1929.     int desc;
  1930.     
  1931.     page_size = getpagesize ();
  1932.     bzero (_dld_symtab, TABSIZE * sizeof(symbol *));
  1933.     _dld_latest_entry = 0;
  1934.     _dld_dummy_entry = 0;
  1935.     dld_undefined_sym_count = 0;
  1936.     _dld_exec_flags_valid = 0;
  1937.     
  1938.     if (myname == 0) {
  1939.     dld_errno = DLD_ENOFILE;
  1940.     return dld_errno;
  1941.     }
  1942.     
  1943.     if (setjmp (_dld_env)) {
  1944.     clean_up ();
  1945.     file_close ();
  1946.     return dld_errno;
  1947.     }
  1948.     _dld_latest_entry = make_entry (myname);
  1949.     desc = file_open (_dld_latest_entry);
  1950. #ifdef atarist
  1951.     st_read_header (desc, _dld_latest_entry);
  1952. #endif /* atarist */
  1953.     read_file_symbols (desc, _dld_latest_entry, 0);
  1954.  
  1955. #ifdef atarist
  1956.     /* do a consistency check of data in entry against data in _base */
  1957.     if ((_dld_latest_entry->header.a_text != _base->p_tlen) ||
  1958.     (_dld_latest_entry->header.a_data != _base->p_dlen) ||
  1959.     (_dld_latest_entry->header.a_bss != _base->p_blen)) {
  1960.     if (_dld_latest_entry->symbols) {
  1961.         free (_dld_latest_entry->symbols);
  1962.         _dld_latest_entry->symbols = 0;
  1963.     }
  1964.     file_close ();
  1965.     dld_errno = DLD_EBADHEADER;
  1966.     return dld_errno;
  1967.     }
  1968.     _dld_latest_entry->text_start_address = (int) _base->p_tbase;
  1969.     _dld_latest_entry->data_start_address = (int) _base->p_dbase;
  1970.     _dld_latest_entry->bss_start_address = (int) _base->p_bbase;
  1971.     relocate_symbol_address (_dld_latest_entry);
  1972. #endif /* atarist */
  1973.  
  1974.     if (_dld_latest_entry->symbols) {
  1975.     free (_dld_latest_entry->symbols);
  1976.     _dld_latest_entry->symbols = 0;
  1977.     }
  1978.     
  1979.     file_close ();
  1980.  
  1981.     _dld_latest_entry->undefined_symbol_count = 0;
  1982.     _dld_latest_entry->all_symbols_resolved_flag = 1;
  1983.     
  1984.     return 0;
  1985. } /* dld_init */
  1986.  
  1987.  
  1988. dld_link (object_file)
  1989. char *object_file;
  1990. {
  1991.     register int desc;
  1992.     struct file_entry *old_latest_entry = _dld_latest_entry;
  1993.  
  1994.     if (setjmp (_dld_env)) {
  1995.     if (old_latest_entry != _dld_latest_entry)
  1996.         clean_up ();
  1997.     file_close ();
  1998.     return dld_errno;
  1999.     }
  2000.  
  2001.     dld_errno = 0;
  2002.     file_close ();            /* file might be opened in the last
  2003.                        call */
  2004.     
  2005.     if (object_file == 0) {
  2006.     dld_errno = DLD_ENOFILE;
  2007.     return dld_errno;
  2008.     }
  2009.     
  2010.     _dld_latest_entry = make_entry (object_file);
  2011.  
  2012.     desc = file_open (_dld_latest_entry);
  2013.     
  2014.     read_file_symbols (desc, _dld_latest_entry, 1);
  2015.  
  2016.     if (_dld_latest_entry->library_flag && _dld_latest_entry->subfiles == 0) {
  2017.     free (_dld_latest_entry->local_sym_name);
  2018.     if (_dld_latest_entry->filename != _dld_latest_entry->local_sym_name)
  2019.         free (_dld_latest_entry->filename);
  2020.     
  2021.     _dld_latest_entry = kill_entry (_dld_latest_entry);
  2022.     file_close ();
  2023.     return 0;
  2024.     }
  2025.     
  2026.     relocate_entry_symbols (desc, _dld_latest_entry);
  2027.     
  2028.     file_close ();
  2029.     
  2030.     _dld_patch_all_files (_dld_latest_entry);
  2031.  
  2032.     _dld_exec_flags_valid = 0;
  2033.     return 0;
  2034. } /* dld_link */
  2035.  
  2036.  
  2037. /* return the location of the given symbol without prepending a '_'. */
  2038. unsigned long
  2039. dld_get_bare_symbol (name)
  2040. char *name;
  2041. {
  2042.     register symbol *sp;
  2043.     
  2044.     if (name == 0)
  2045.     return 0;
  2046.  
  2047.     sp = _dld_getsym_soft (name);
  2048.  
  2049.     if (sp)
  2050.     if (sp->defined)
  2051.         return sp->value;
  2052.     return 0;
  2053. } /* dld_get_bare_symbol */
  2054.  
  2055.     
  2056. /* given a file_entry, unlink that (and all its decendents).  Modules still
  2057.    referenced by the remainings will not be unlinked.
  2058.    ENTRY is assumed to be a valid pointer to a file_entry structure.
  2059.    if FORCE is true, remove this entry event regardless if it is still
  2060.    referenced by others */
  2061. void
  2062. _dld_unlink_entry (entry, force)
  2063. struct file_entry *entry;
  2064. int force;
  2065. {
  2066.     if (force) {
  2067.     entry->ref_count = 0;
  2068.     if (entry->library_flag) {
  2069.         register struct file_entry *subentry = entry->subfiles;
  2070.         for (; subentry; subentry = subentry->chain)
  2071.         subentry->ref_count = 0;
  2072.     }
  2073.     } else {
  2074.     if (entry->library_flag || entry->already_unlink)
  2075.         return;
  2076.     entry->ref_count--;
  2077.     entry->already_unlink = 1;
  2078.     }
  2079.  
  2080.     if (entry->ref_count == 0) {
  2081.     remove_cross_references (_dld_latest_entry, entry);
  2082.  
  2083.     cleanup_obsolete_entries (entry);
  2084.  
  2085.     cleanup_symtab ();
  2086.  
  2087.     _dld_latest_entry = do_unlink (_dld_latest_entry);
  2088.     }
  2089.  
  2090.     _dld_patch_all_files (_dld_latest_entry);
  2091.     _dld_exec_flags_valid = 0;
  2092. } /* _dld_unlink_entry */
  2093.  
  2094.  
  2095. /*
  2096.  * return true if the named function can be safely exeucted.
  2097.  */
  2098. dld_function_executable_p (name)
  2099. char name[];
  2100. {
  2101.     register symbol *sp;
  2102.     register char *p;
  2103.  
  2104.     if (name == 0)
  2105.     return 0;
  2106.  
  2107.     if (setjmp (_dld_env))
  2108.     return 0;
  2109.  
  2110.     /* prepend an '_' to name, as required by the C convention */
  2111.     p = (char *) _dld_malloc (strlen(name) + 2);
  2112.     *p = '_';
  2113.     strcpy (p+1, name);
  2114.     sp = _dld_getsym_soft (p);
  2115.     free (p);
  2116.  
  2117.     if (sp && sp->defined == (N_EXT | N_TEXT)) {
  2118.     register struct file_entry *fe = sp->defined_by;
  2119.  
  2120.     if (fe == 0) return 0;
  2121.     
  2122.     if (!_dld_exec_flags_valid)
  2123.         find_all_executable_modules ();
  2124.  
  2125.     return fe->executable_flag;
  2126.     }
  2127.  
  2128.     return 0;
  2129. } /* dld_function_executable_p */
  2130.